package Demo7;

import java.util.Random;

public class Demo7 {
    public static long count1 = 0;
    public static long count2 = 0;

    public static void main(String[] args) throws InterruptedException {

        // 记录开始时间
        long s1 = System.currentTimeMillis();

        Random random = new Random();
        int[] arr = new int[100000000];
        for(int i = 0;i < 100000000;i++) {
            arr[i] = random.nextInt(100) + 1 ;
        }

        //线程t1
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 100000000; i += 2) {
                count1 += arr[i];
            }
        },"线程t1计算偶数");

        //线程t2
        Thread t2 = new Thread(() -> {
            for (int i = 1; i < 100000000; i += 2) {
                count2 += arr[i];
            }
        },"线程t2计算奇数");

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        //记录结束时间
        long s2 = System.currentTimeMillis();

        System.out.println(count1 + count2);
        System.out.println(s2 - s1);

    }
}
